//
//  TripCtrl.m
//  LvXingPai
//
//  Created by _Junxee on 16/8/13.
//  Copyright © 2016年 _Junxee. All rights reserved.
//

#import "TripCtrl.h"
#import "TripCell.h"
#import "TripModel.h"

@interface TripCtrl ()
@property(nonatomic,strong) NSMutableArray *dataArr;

@end

@implementation TripCtrl

- (NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarButStyle];
    [self loadData];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"全部游记";
}

-(void)setBarButStyle{
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_icon_navigation_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:nil];
    rightBtn.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = leftBtn;
    self.navigationItem.rightBarButtonItem = rightBtn;
}

#pragma mark -- 请求数据
-(void)loadData{
    AFHTTPRequestOperationManager *Manger = [AFHTTPRequestOperationManager manager];
    Manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [Manger GET:[NSString stringWithFormat:@"http://api.lvxingpai.com/app/travelnotes?imgWidth=200&locality=%@&page=0&pageSize=15",_tModel.pushId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dict[@"result"];
        for (NSDictionary *dict2 in arr) {
            
            TripModel * trModel = [[TripModel alloc]init];
            trModel.authorName = dict2[@"authorName"];
            trModel.title = dict2[@"title"];
            trModel.summary = dict2[@"summary"];
            trModel.authorAvatar = dict2[@"authorAvatar"];
            [self.dataArr addObject:trModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"f");
    }];
}

-(void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)setTModel:(DestinModel *)tModel{
    _tModel = tModel;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TripCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TripModel * model = self.dataArr[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.nameLabel.text = model.authorName;
    cell.desLabel.text = model.summary;
    [cell.leftImageV sd_setImageWithURL:[NSURL URLWithString:model.authorAvatar] placeholderImage:[UIImage imageNamed:@"avatar_default"]];
    return cell;
}




@end
